package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;
import java.awt.TextField;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class MyJUpdatePanel extends JPanel{

	private static final long serialVersionUID = 1L;
	private JButton updateButton, deleteButton, insertButton, exitButton, clearButton;
	private TextField studentField, cijferField;
	private JLabel errorLabel;
	private JTable table;
	private TableColumn col;
	private MyJTableModel model;
	private Vector<Vector<String>> data = new Vector<Vector<String>>();
	
	public MyJUpdatePanel(Control control) { 
		this.setLayout(new BorderLayout());
		
		Panel tablePanel = new Panel();
		Panel commandPanel = new Panel();
		
		tablePanel.setBounds(20, 20, 400, 100);
		
		Vector<String> columnNames = new Vector<String>();
	    columnNames.addElement("Naam");
	    columnNames.addElement("Cijfer");

	    model = new MyJTableModel();
	    table = new JTable(data, columnNames);
	    table.setModel(model);
		table.setEnabled(true);
		table.addMouseListener(control);
		model.setDataVector(data, columnNames);
		
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
		table.setRowSorter(sorter);

		JScrollPane scrollPane = new JScrollPane(table);
		tablePanel.add(scrollPane);
		this.add(tablePanel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		col = table.getColumnModel().getColumn(0);
	    col.setPreferredWidth(150);
	    col = table.getColumnModel().getColumn(1);
	    col.setPreferredWidth(150);
	    
        table.setPreferredScrollableViewportSize(new Dimension(445, 170));
        table.setFillsViewportHeight(false);
        table.setDragEnabled(false);
        table.getTableHeader().setReorderingAllowed(false);

		commandPanel.setLayout( null );
		commandPanel.setBounds(20, 20, 400, 150);
		
		errorLabel = new JLabel();
		errorLabel.setBounds(20, 5, 250, 15);
		errorLabel.setForeground(Color.RED);
		commandPanel.add(errorLabel);
		
		insertButton = new JButton("Insert");
		insertButton.setBounds(250, 25, 100, 20);
		insertButton.addActionListener(control);
		commandPanel.add(insertButton);
		
		updateButton = new JButton("Update");
		updateButton.setBounds(250, 50, 100, 20);
		updateButton.addActionListener(control);
		commandPanel.add(updateButton);
		
		deleteButton = new JButton("Delete");
		deleteButton.setBounds(250, 75, 100, 20);
		deleteButton.addActionListener(control);
		commandPanel.add(deleteButton);
		
		exitButton = new JButton("Exit");
		exitButton.setBounds(250, 100, 100, 20);
		exitButton.addActionListener(control);
		commandPanel.add(exitButton);
		
		clearButton = new JButton("Clear");
		clearButton.setBounds(130, 50, 100, 20);
		clearButton.addActionListener(control);
		commandPanel.add(clearButton);
				
		
		studentField = new TextField();
		studentField.setBounds(20, 26, 150, 20);
		commandPanel.add(studentField);
	
		cijferField = new TextField();
		cijferField.setBounds(180, 26, 50, 20);
		commandPanel.add(cijferField);		
		
		this.add(commandPanel, BorderLayout.SOUTH);
	}
	
	public String getValueAt(int row, int column) {
		return (String) table.getValueAt(row, column);
	}
	
	public Vector<Vector<String>>getData() {
		return data;
	}
	
	public void addRow(String naam, String cijfer) {
		Vector<String> v = new Vector<String>();
		v.add(naam);
		v.add(cijfer);
		this.data.add(v);
		Vector<String> columnNames = new Vector<String>();
	    columnNames.addElement("Naam");
	    columnNames.addElement("Cijfer");
		model.setDataVector(data, columnNames);
		
	}

	public int getSelectedRow() {
		return this.table.getSelectedRow();
		
	}
	
	public void setTextField(int i, String value) {
		if( i == 1 ) {
			studentField.setText(value);
		} else if( i == 2 ) {
			cijferField.setText(value);
		}
		this.repaint();
	}
	
	public void clearFields() {
		studentField.setText("");
		cijferField.setText("");
		this.setError("");
		table.clearSelection();
	}
	
	public void setError(String text) {
		errorLabel.setText(text);
		errorLabel.repaint();
		errorLabel.validate();
	}

	public HashMap<String, String> getFields() {
		HashMap<String, String> hm = new HashMap<String, String>();	
		hm.put("student", studentField.getText());
		hm.put("cijfer", cijferField.getText());	
		return hm;	
	}

	class MyJTableModel extends DefaultTableModel {

		private static final long serialVersionUID = 1L;

		@Override
		public boolean isCellEditable(int row, int col) {
			return false;
		}
	}
		
}
